package com.controller;


import com.beans.Comment;
import com.beans.ResponseResult;
import com.beans.User;
import com.service.CommentService;
import com.vo.CommentVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/comment")
public class CommentController {

    @Resource
    CommentService commentService;

    @RequestMapping("/addComment")
    @ResponseBody
    ResponseResult addComment(Comment comment, HttpSession request){
        User user = (User)request.getAttribute("userInfo");
        if(user == null){
            return ResponseResult.error("请先登录");
        }
        Long id = user.getId();
        comment.setUserId(id.intValue());
        return commentService.addComment(comment);
    }

    @RequestMapping("/deleteComment")
    public String deleteComment(Comment comment){
        commentService.deleteComment(comment.getId());
        return "forward:/comment/getAllComment";
    }

    @RequestMapping("delCommentByUser")
    @ResponseBody
    public ResponseResult delCommentByUser(Comment comment){
        System.out.println("==========="+comment.getId()+"=================");
        System.out.println("==========="+comment.toString()+"=================");
        return commentService.deleteComment(comment.getId());
    }

    @RequestMapping("/getAllComment")
    public String getAllComment(Model model){
        model.addAttribute("list",commentService.getAllComment());
        return "forum/all-comment";
    }


}
